package com.example.demo.saToken.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName JurAuthController
 * @Description 权限校验控制器
 * @Author MingChang.Wei
 * @Date 2024/9/19
 **/
@RestController
@RequestMapping("/jur/")
@Tag(name = "权限管理", description = "用户权限和角色管理接口")
public class JurAuthController {
    @RequestMapping("getPermission")
    @Operation(summary = "获取权限列表", description = "获取当前用户的所有权限和角色")
    public SaResult getPermission() {
        List<String> permissionList = StpUtil.getPermissionList();
        System.out.println("当前登录账号拥有的所有权限：" + permissionList);

        List<String> roleList = StpUtil.getRoleList();
        System.out.println("当前登录账号拥有的所有角色：" + roleList);

        return SaResult.ok().set("permissionList", permissionList).set("roleList", roleList);
    }

    @RequestMapping("checkPermission")
    @Operation(summary = "检查权限", description = "检查用户是否具有指定权限")
    public SaResult checkPermission() {
        StpUtil.hasPermission("user.add");
        StpUtil.hasPermissionAnd("user.add", "user.delete", "user.get");
        StpUtil.hasPermissionOr("user.add", "user.delete", "user.get");

        StpUtil.checkPermission("user.add");
        StpUtil.checkPermissionAnd("user.add", "user.delete", "user.get");
        StpUtil.hasPermissionOr("user.add", "user.delete", "user.get");

        return SaResult.ok();
    }

    @RequestMapping("checkRole")
    @Operation(summary = "检查角色", description = "检查用户是否具有指定角色")
    public SaResult checkRole() {
        StpUtil.hasRole("admin");
        StpUtil.hasRoleAnd("admin", "ceo", "cfo");
        StpUtil.hasRoleOr("admin", "ceo", "cfo");

        StpUtil.checkRole("admin");
        StpUtil.checkRoleAnd("admin", "ceo", "cfo");
        StpUtil.checkRoleOr("admin", "ceo", "cfo");

        return SaResult.ok();
    }

    @RequestMapping("wildcardPermission")
    @Operation(summary = "通配符权限检查", description = "使用通配符模式检查权限")
    public SaResult wildcardPermission() {
        StpUtil.hasPermission("art.add");
        StpUtil.hasPermission("art.delete");
        StpUtil.hasPermissionOr("goods.add");

        return SaResult.ok();
    }
}
